﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Koronkirskuri
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private double loan;
        private double intrestRate;
        private double timeLeft;
        private double payment;


        public MainWindow()
        {
            InitializeComponent();
            Initialize();
        }

        private void Initialize()
        {
            loan = 0;
            intrestRate = 0;
            timeLeft = 0;
            payment = 0;
        }

        private void btnCalculate_Click(object sender, RoutedEventArgs e)
        {
            lbLoanList.Items.Clear();
            tbPayment.Text = "";
            if (LoadNums())
            {
                int j = 0;
                List<double> loanAmount = Calculate();
                tbPayment.Text = payment.ToString();
                for (int i = 0; i < timeLeft * 12; i++)
                {
                    if (i % 12 == 0 && i != 0)
                    {
                        j++;
                    }
                    lbLoanList.Items.Add(loanAmount[j]);
                    loanAmount[j] -= payment;
                }
                
                

            }
        }

        private bool LoadNums()
        {
            if (!(double.TryParse(tbLoan.Text, out loan))) return false;
            if (!(double.TryParse(tbIntrest.Text, out intrestRate))) return false;
            if (!(double.TryParse(tbTime.Text, out timeLeft))) return false;
            return true;
        }

        private List<double> Calculate()
        {
            List<double> l = new List<double>(); //loan
            List<double> a = new List<double>(); //amount

            //l.Add(loan);
            //area 1
            if (intrestRate != 0)
            {
                l.Add(loan + loan / 12 * (1 + intrestRate / 100));
            }
            else
            {
                l.Add(loan);
            }
            
            a.Add(l.Last() * 12 + l[l.Count - 1] * (1 + intrestRate / 100));
            //a.Add(l.Last() * 12);
            

            if (timeLeft > 1)
            {
                for (int i = 0; i < timeLeft - 1; i++)
                {
                    //area 2 or more
                    a.Add(l.Last() * 12 + l[l.Count - 1] * (1 + intrestRate / 100));
                    if (loan != 0)
                    {
                        l.Add(loan + loan / 12 * (1 + intrestRate / 100));
                    }
                    else
                    {
                        l.Add(loan);
                    }
                }
            }

            //triangle area
            double t_a = timeLeft * 12 * a.Last() / 2;

            //slope
            payment = a.Last() / (timeLeft * 12) / 12;

            return l;
        }
    }
}
